<?php
include('lib/formatName.php');
require_once "HTML/Template/IT.php";

function GetSpouseAndKids($myID)
{
	global $rpt;

	$query = "SELECT * FROM person WHERE `id` = $myID";
	$myresult = mysql_query($query) or die("Query failed : " . mysql_error());
	$myData = mysql_fetch_array($myresult, MYSQL_ASSOC);
	
	$myFull = formatName($myData['first'], $myData['middle'], $myData['last'], $myData['suffix'], $myData['nick'], "fnmls");
	if ($myData['sex'] == "M") {
		$asSpouseFld = "husband";
		$mySpouseFld = "wife";
	} else {
		$asSpouseFld = "wife";
		$mySpouseFld = "husband";
	}
	
	$query = "SELECT $mySpouseFld FROM marriage WHERE `" .$asSpouseFld. "` = $myID ORDER BY date";
	$marriage_result = mysql_query($query) or die("Query failed : " . mysql_error());

	$rpt .= "<ul>\n";
	
	while ($aMarriage = mysql_fetch_array($marriage_result, MYSQL_ASSOC)) {
		$mySpouseID = $aMarriage["$mySpouseFld"];

		$query = "SELECT * FROM person WHERE `id` = $mySpouseID";
		$mySpouse_result = mysql_query($query) or die("Query failed : " . mysql_error());
		$mySpouseData = mysql_fetch_array($mySpouse_result, MYSQL_ASSOC);

		$mySpouseFull = formatName($mySpouseData['first'], $mySpouseData['middle'], $mySpouseData['last'], $mySpouseData['suffix'], $mySpouseData['nick'], "fnmls");
		$rpt .= "<li><b>$myFull <==> <a href=\"person.php?id=$mySpouseData[id]\">$mySpouseFull</a></b></li>\n";
		GetSpouseKids($myID, $mySpouseID, $myData['sex']);
	}
	$rpt .= "</ul>\n";
}

function GetSpouseKids($myID, $spouseID, $mySex)
{
	global $rpt;

	if ($mySex == "M") {
		$asParentFld  = "father";
		$othParentFld = "mother";
	} else {
		$asParentFld  = "mother";
		$othParentFld = "father";
	}

	$query = "SELECT * FROM person WHERE `".$asParentFld."` = $myID AND `$othParentFld` = $spouseID ORDER BY dob";
	$kids_result = mysql_query($query) or die("Query failed : " . mysql_error());

	$rpt .= "<ol>\n";

	while ($aKid = mysql_fetch_array($kids_result, MYSQL_ASSOC)) {
		$aKidID = $aKid['id'];

		$query = "SELECT * FROM person WHERE `id` = $aKidID";
		$aKid_result = mysql_query($query) or die("Query failed : " . mysql_error());
		$aKidData = mysql_fetch_array($aKid_result, MYSQL_ASSOC);

		$aKidFull = formatName($aKidData['first'], $aKidData['middle'], $aKidData['last'], $aKidData['suffix'], $aKidData['nick'], "fnmls");
		$rpt .= "<li><a href=\"person.php?id=$aKidData[id]\">$aKidFull</a></li>\n";
		GetSpouseAndKids($aKidData['id']);
	}
	$rpt .= "</ol>\n";

}


// GET HTTP "GET" VARS

if ( isset($_GET['id']) ) {
        $id = $_GET['id'];
} else {
        $id = 3054;
}
$id=preg_replace("/[^0-9]/","",$id);  //get rid of anything but numbers
if ($id < 1 || $id > pow(2,17)) {
        die("ID is out of range.");
}

// SET UP TEMPLATE

$tpl = new HTML_Template_IT("templates");
$status = $tpl->loadTemplatefile("report_menu_tmpl.html", true, true);

/* Connecting, selecting database */
$link = mysql_connect("localhost", "ftreeweb", "ftree_web")
   or die("Could not connect : " . mysql_error());
mysql_select_db("ftree") or die("Could not select database");


// GET PERSON

$query = "SELECT * FROM person WHERE `id` = $id";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$idData = mysql_fetch_array($result, MYSQL_ASSOC);

$full = formatName($idData['first'], $idData['middle'], $idData['last'], $idData['suffix'], $idData['nick'], "fnmls");

$tpl->setVariable("PersonID", $id);
$tpl->setVariable("FullName", $full);

$rpt = "<h2>Descendents:</h2>\n";

GetSpouseAndKids($id);

$tpl->setVariable("ReportData", $rpt);

mysql_close($link);

$tpl->show();
?>
